package com.jichangxiu.server.platform.utils;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.jichangxiu.common.entity.bo.Record;
import com.jichangxiu.server.platform.entity.po.PlatformMenu;
import com.jichangxiu.server.platform.entity.po.PlatformMenuPower;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * MenuUtils:
 *
 * @author Jcx
 * @create 2023-03-17 20:28
 */
public class MenuUtils {

    /**
     * 给对应菜单列表里的菜单加上对应权限
     *
     * @param platformMenuList      菜单列表
     * @param platformMenuPowerList 权限列表
     */
    public static void addPowerToMenu(List<PlatformMenu> platformMenuList, List<PlatformMenuPower> platformMenuPowerList) {
        platformMenuList.forEach(platformMenu -> {
            List<PlatformMenuPower> platformMenuPowers = new ArrayList<>();
            platformMenuPowerList.forEach(platformMenuPower -> {
                if (platformMenu.getMenuId().equals(platformMenuPower.getMenuId())) {
                    platformMenuPowers.add(platformMenuPower);
                }
            });
            platformMenu.setPlatformMenuPowers(platformMenuPowers);
        });
    }

    /**
     * 将菜单列表的ID抽取出来
     *
     * @param platformMenuList 菜单列表
     * @return 菜单ID列表
     */
    public static List<String> getMenuIds(List<PlatformMenu> platformMenuList) {
        List<String> menuIds = new ArrayList<>();
        if (ObjectUtil.isNotEmpty(platformMenuList)) {
            platformMenuList.forEach(platformMenu -> menuIds.add(platformMenu.getMenuId()));
        }
        return menuIds;
    }

    /**
     * 菜单转换
     *
     * @param platformMenuList 菜单列表
     * @return 转换后的菜单列表
     */
    public static List<Record> convertMenu(List<PlatformMenu> platformMenuList) {
        if (ObjectUtil.isEmpty(platformMenuList)) {
            return new ArrayList<>();
        }
        platformMenuList = platformMenuList.stream().sorted(Comparator.comparing(b -> BigDecimal.valueOf(b.getSort()))).collect(Collectors.toList());
        List<Record> recordByList = Record.getRecordByList(platformMenuList);
        if (ObjectUtil.isNotEmpty(recordByList)) {
            recordByList.forEach(record -> {
                Record meta = Record.getRecord();
                meta.set("title", record.getStr("metaTitle"));
                meta.set("icon", record.getStr("metaIcon"));
                meta.set("type", record.getStr("metaType"));
                if (record.getBoolean("metaHidden")) {
                    meta.set("hidden", record.getBoolean("metaHidden"));
                }
                if (record.getBoolean("metaAffix")) {
                    meta.set("affix", record.getBoolean("metaAffix"));
                }
                if (record.getBoolean("metaHiddenBreadcrumb")) {
                    meta.set("hiddenBreadcrumb", record.getBoolean("metaHiddenBreadcrumb"));
                }
                if (StrUtil.isNotBlank(record.getStr("metaActive"))) {
                    meta.set("active", record.getStr("metaActive"));
                }
                if (record.getBoolean("metaFullPage")) {
                    meta.set("fullpage", record.getBoolean("metaFullPage"));
                }
                if (StrUtil.isNotBlank(record.getStr("metaColor"))) {
                    meta.set("color", record.getStr("metaColor"));
                }
                if (StrUtil.isNotBlank(record.getStr("metaTag"))) {
                    meta.set("tag", record.getStr("metaTag"));
                }
                record.put("meta", meta);
                if (StrUtil.isBlank(record.getStr("redirect"))) {
                    record.remove("redirect");
                }
                if (StrUtil.isBlank(record.getStr("component"))) {
                    record.remove("component");
                }
                record.remove("deleted");
                record.remove("metaTitle");
                record.remove("metaHidden");
                record.remove("metaAffix");
                record.remove("metaIcon");
                record.remove("metaType");
                record.remove("metaHiddenBreadcrumb");
                record.remove("metaActive");
                record.remove("metaColor");
                record.remove("metaTag");
                record.remove("metaFullPage");

                List<Record> apiList = new ArrayList<>();
                List<PlatformMenuPower> platformMenuPowerList = record.getList("platformMenuPowerList", PlatformMenuPower.class);
                if (ObjectUtil.isNotEmpty(platformMenuPowerList)) {
                    platformMenuPowerList.forEach(platformMenuPower -> apiList.add(Record.getRecord("powerId", platformMenuPower.getPowerId()).set("powerCode", platformMenuPower.getPowerCode()).set("powerName", platformMenuPower.getPowerName()).set("powerPath", platformMenuPower.getPowerPath())));
                }
                record.set("apiList", apiList);
            });
        }
        return Record.listToTree(recordByList, "menuId", "parentId", "children");
    }

    public static Record convertMenu(PlatformMenu platformMenu) {
        if (ObjectUtil.isEmpty(platformMenu)) {
            return Record.getRecord();
        }
        Record record = Record.getRecordByModel(platformMenu);
        Record meta = Record.getRecord();
        meta.set("title", record.getStr("metaTitle"));
        meta.set("icon", record.getStr("metaIcon"));
        meta.set("type", record.getStr("metaType"));
        meta.set("hidden", record.getBoolean("metaHidden"));
        meta.set("affix", record.getBoolean("metaAffix"));
        meta.set("hiddenBreadcrumb", record.getBoolean("metaHiddenBreadcrumb"));
        meta.set("active", record.getStr("metaActive"));
        meta.set("fullpage", record.getBoolean("metaFullPage"));
        meta.set("color", record.getStr("metaColor"));
        meta.set("tag", record.getStr("metaTag"));
        record.put("meta", meta);

        record.remove("deleted");
        record.remove("metaTitle");
        record.remove("metaHidden");
        record.remove("metaAffix");
        record.remove("metaIcon");
        record.remove("metaType");
        record.remove("metaHiddenBreadcrumb");
        record.remove("metaActive");
        record.remove("metaColor");
        record.remove("metaTag");
        record.remove("metaFullPage");

        List<Record> apiList = new ArrayList<>();
        List<PlatformMenuPower> platformMenuPowerList = record.getList("platformMenuPowers", PlatformMenuPower.class);
        if (ObjectUtil.isNotEmpty(platformMenuPowerList)) {
            platformMenuPowerList.forEach(platformMenuPower -> apiList.add(Record.getRecord("powerId", platformMenuPower.getPowerId()).set("powerCode", platformMenuPower.getPowerCode()).set("powerName", platformMenuPower.getPowerName()).set("powerPath", platformMenuPower.getPowerPath())));
        }
        record.set("apiList", apiList);
        record.remove("platformMenuPowers");
        return record;
    }

}
